package ru.st.selenium;

import java.util.List;

import org.testng.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Application extends ru.st.selenium.pages.TestBase {

   
//Functions for tests 
  public void loginEditor(WebDriver driver) throws Exception {
	    //driver.get(baseUrl + "/php4dvd/");
	    driver.findElement(By.id("username")).clear();
	    driver.findElement(By.id("username")).sendKeys("editor");
	    driver.findElement(By.name("password")).clear();
	    driver.findElement(By.name("password")).sendKeys("Qwer12345");
	    driver.findElement(By.name("submit")).click();

		  } 
  public void loginAdmin(WebDriver driver) throws Exception {
	    //driver.get(baseUrl + "/php4dvd/");
	    driver.findElement(By.id("username")).clear();
	    driver.findElement(By.id("username")).sendKeys("admin");
	    driver.findElement(By.name("password")).clear();
	    driver.findElement(By.name("password")).sendKeys("Qwer54321");
	    driver.findElement(By.name("submit")).click();
	      }
  
  public void loginRandom(WebDriver driver) throws Exception {
	    //driver.get(baseUrl + "/php4dvd/");
	    driver.findElement(By.id("username")).clear();
	    driver.findElement(By.id("username")).sendKeys("123");
	    driver.findElement(By.name("password")).clear();
	    driver.findElement(By.name("password")).sendKeys("456");
	    driver.findElement(By.name("submit")).click();
	      }
  
  public void logout(WebDriver driver) throws Exception {
	  
	  driver.findElement(By.cssSelector("nav a[href $= '?logout']")).click();
	  WebDriverWait wait = new WebDriverWait(driver, 30);
	  wait.until(ExpectedConditions.alertIsPresent());
	  Alert alert = driver.switchTo().alert();
	  alert.accept();
	  }
  
  
  public void assert_is_logged_in(WebDriver driver) throws Exception {
	  WebElement element = driver.findElement(By.cssSelector("nav"));
	  Assert.assertTrue(element.isDisplayed());
	 }
  
  public void assert_is_not_logged_in(WebDriver driver) throws Exception {
	  WebElement element =  driver.findElement(By.id("loginform"));
	  Assert.assertTrue(element.isDisplayed());
	  }

  public void addMovieAllRequiredFields(WebDriver driver) throws Exception {
		// add movie
		driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
		driver.findElement(By.name("name")).clear();
		driver.findElement(By.name("name")).sendKeys("Iron Man 3");
		driver.findElement(By.name("year")).clear();
		driver.findElement(By.name("year")).sendKeys("2014");
		driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
		}

	public void go_to_collection(WebDriver driver) throws Exception {
		WebDriverWait wait = new WebDriverWait(driver, 30);
		wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("nav")));
		driver.findElement(By.cssSelector("h1")).click();
		}

	public void addMovieImdbSearch(WebDriver driver) throws Exception {
		// add movie by IMDB search
	    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
	    driver.findElement(By.id("imdbsearch")).clear();
	    driver.findElement(By.id("imdbsearch")).sendKeys("Iron Man 3");
	    driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
	    driver.findElement(By.xpath(".//*[@id='content']/section/div/div/table/tbody/tr[1]/td[2]/a")).click();
	    driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
	    }

	public void addMovieWithoutTitle(WebDriver driver) throws Exception {
		// add movie with no title
		driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
		driver.findElement(By.name("name")).clear();
		driver.findElement(By.name("name")).sendKeys("");
		driver.findElement(By.name("year")).clear();
		driver.findElement(By.name("year")).sendKeys("2014");
		driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
		}

	public void addMovieWithoutYear(WebDriver driver) throws Exception {
		// add movie with no year
		driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
		driver.findElement(By.name("name")).clear();
		driver.findElement(By.name("name")).sendKeys("Iron Man 3");
		driver.findElement(By.name("year")).clear();
		driver.findElement(By.name("year")).sendKeys("");
		driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
		}

	public int count_movies(WebDriver driver) throws Exception {
		int count = 0;
		WebDriverWait wait = new WebDriverWait(driver, 30);
		List<WebElement> movies = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("movie_box")));
		for (int i = 0; i < movies.size(); i++){
		count += 1;	}
		return count;
	  }
	
	public void deleteMovie(WebDriver driver) throws Exception {
		  WebElement element = driver.findElement(By.className("movie_box"));
		  driver.findElement(By.className("movie_box")).click();
		  driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();	
		  WebDriverWait wait = new WebDriverWait(driver, 10);
		  wait.until(ExpectedConditions.alertIsPresent());
		  Alert alert = driver.switchTo().alert();
		  alert.accept();
		  wait.until(ExpectedConditions.stalenessOf(element));
		  driver.findElement(By.cssSelector("h1")).click();
		}    
	  public WebElement remember_element(WebDriver driver) throws Exception {
		  WebElement element = driver.findElement(By.className("movie_box"));
			return element;
		 }
	  
	  public WebElement message_element(WebDriver driver) throws Exception {
		  WebElement element = driver.findElement(By.className("content"));
			return element;
		 }

	  
	  public void wait_until_remembered_element_is_disappeared(WebDriver driver, WebElement element) throws Exception {
			WebDriverWait wait = new WebDriverWait(driver, 30);
			wait.until(ExpectedConditions.stalenessOf(element));
			
		}
	  public void wait_until_new_elements_are_appeared(WebDriver driver) throws Exception {
			WebDriverWait wait = new WebDriverWait(driver, 30);
			wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("movie_box")));
		}

	  public void wait_until_no_results_are_appeared(WebDriver driver) throws Exception {
			WebDriverWait wait = new WebDriverWait(driver, 30);
			wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("content")));
		}

	  public void search_movie(WebDriver driver, String text) throws Exception {
		    driver.findElement(By.id("q")).clear();
		    driver.findElement(By.id("q")).sendKeys(text, Keys.RETURN);
			}

	  public void add_movie(WebDriver driver, String title) throws Exception {
			// add movie
			driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
			driver.findElement(By.name("name")).clear();
			driver.findElement(By.name("name")).sendKeys(title);
			driver.findElement(By.name("year")).clear();
			driver.findElement(By.name("year")).sendKeys("1111");
			driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();
			}	  
}